package my2Cents.tools;

import java.awt.event.MouseEvent;

import my2Cents.Sandbox;
import my2Cents.Util;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.PPolygon;

public class GearTool extends CircleTool {
	private float size;
	protected int mode = 0;
	
	public GearTool() {
		setName("Gear");
		setDescription(
				"Click and drag to create a Gear."
			);
	}
	
	public void startTool(MouseEvent arg0) {
		super.startTool(arg0);
		shapePos = new Vector2f(arg0.getX(), arg0.getY());
		size = 0;
	}
	
	public void toolThink(MouseEvent arg0) {
		Vector2f vec = new Vector2f(arg0.getX(), arg0.getY());
		size = shapePos.distance(vec);
		if(mode == 0) {
			shape = gear(size, (float) Util.angleVec(shapePos, vec));
		} else {
			shape = gearInverse(size, (float) Util.angleVec(shapePos, vec));
		}
	}
	
	public void endTool(MouseEvent arg0) {
		if(shape != null) {
			float mass = size*2;
			if(mode == 1) {
				mass -= (mass/2.5);
			}
			Body b = createBody(mass);
			b.setPosition(shapePos.x, shapePos.y);
			sandbox.postBodyCreated(b);
			sandbox.onToolEnd();
		}
		super.endTool(arg0);
	}
	
	/*public void drawGhost(Graphics2D g) {
		if(!started) return;
		if(circle != null && 
				circle.getVertices() != null &&
				circle.getVertices().length > 0) {
			int lx = 0;
			int ly = 0;
			int cx = (int)startPos.x;
			int cy = (int)startPos.y;
			for(int i=0; i<circle.getVertices().length; i++) {
				Vector2f vec = (Vector2f) circle.getVertices()[i];
				if(i > 0) {
					g.drawLine((int)vec.x + cx, (int)vec.y + cy, lx, ly);
				}
				lx = (int)vec.x + cx;
				ly = (int)vec.y + cy;
			}
		}
	}*/
	
	private PPolygon gear(float size, float rotation) {
		int size2 = (int) (size/4);
		size = size2*4;
		int noVerts = (int) ((size));

		Vector2f[] circleVerts = new Vector2f[noVerts];
		float[] radius = {10+size,size,size,10+size};
		for( int i = 0; i < noVerts; i++ ) {
			float angle = (float) (i* 2 * Math.PI/noVerts);
			circleVerts[i] = new Vector2f(
					(float) (Math.cos(angle + rotation) * radius[i%radius.length]), 
					(float) (Math.sin(angle + rotation) * radius[i%radius.length]));
		}
		if(circleVerts.length > 3) {
			PPolygon circlePolygon = new PPolygon(circleVerts);
			circlePolygon.setName("Gear");
			return circlePolygon;
		}
		return null;
	}
	
	private PPolygon gearInverse(float size, float rotation) {
		//int noVerts = 120;
		
		int size2 = (int) (size/4);
		size = size2*4;
		int noVerts = (int) ((size));
		int outerCircleVerts = noVerts/4;
		
		Vector2f[] circleVerts = new Vector2f[outerCircleVerts+1 + noVerts+1];
		for( int i = 0; i <= outerCircleVerts; i++ ) {
			float angle = (float) (i* 2 * Math.PI/outerCircleVerts);
			circleVerts[i] = new Vector2f(
					(float) (Math.cos(angle + rotation) * (size + 18)), 
					(float) (Math.sin(angle + rotation) * (size + 18)));
		}
		float[] radius = {10+size,size,size,10+size};
		for( int i = 0; i <= noVerts; i++ ) {
			float angle = (float) (i* 2 * Math.PI/noVerts);
			circleVerts[outerCircleVerts+1 + noVerts-i] = new Vector2f(
					(float) (Math.cos(angle + rotation) * radius[i%radius.length]), 
					(float) (Math.sin(angle + rotation) * radius[i%radius.length]));
		}
		if(circleVerts.length > 3) {
			PPolygon circlePolygon = new PPolygon(circleVerts);
			circlePolygon.setName("Inverted Gear");
			return circlePolygon;
		}
		return null;
	}

}
